﻿using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Admin 的摘要说明
/// </summary>
public class Admin
{
	public Admin()
	{
		//
		// TODO: 在此处添加构造函数逻辑
		//
	}
    //查询用户总数
    public int GetAdminCount()
    {
        OleDbConnection myconn = new OleDbConnection(DB.conn);
        OleDbCommand mycmd = new OleDbCommand("select count(*) from [X-admin] ", myconn);
        int sCount = -1;
        try
        {
            myconn.Open();
            sCount = (int)mycmd.ExecuteScalar();
        }
        catch (OleDbException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        finally
        {
            myconn.Close();
        }
        return sCount;
    }

    //用户验证
    public OleDbDataReader CheckAdmin(string sUserName, string sPassWord)
    {
        OleDbConnection myconn = new OleDbConnection(DB.conn);
        OleDbCommand mycmd = new OleDbCommand("select * from [X-admin] where Auser=@admin_login and Apwd=@admin_pwd ", myconn);
        mycmd.Parameters.AddWithValue("@admin_login", sUserName);
        mycmd.Parameters.AddWithValue("@admin_pwd", sPassWord);
        OleDbDataReader dr = null;
        try
        {
            ///打开链接
            myconn.Open();
            ///读取数据
            dr = mycmd.ExecuteReader(CommandBehavior.CloseConnection);
        }
        catch (OleDbException ex)
        {
            ///抛出异常
            throw new Exception(ex.Message, ex);
        }
        ///返回DataReader
        return dr;
    }
    //用户验证
    public int CheckAdminRetInt(string sUserName, string sPassWord)
    {
        OleDbConnection myconn = new OleDbConnection(DB.conn);
        OleDbCommand mycmd = new OleDbCommand("select Count(*) from [X-admin] where Auser=@admin_login and Apwd=@admin_pwd ", myconn);
        mycmd.Parameters.AddWithValue("@admin_login", sUserName);
        mycmd.Parameters.AddWithValue("@admin_pwd", sPassWord);
        int s = 0;
        try
        {
            ///打开链接
            myconn.Open();
            ///读取数据
            s = int.Parse(mycmd.ExecuteScalar().ToString());
        }
        catch (OleDbException ex)
        {
            ///抛出异常
            throw new Exception(ex.Message, ex);
        }
        finally
        {
            myconn.Close();
        }
        ///返回DataReader
        return s;
    }
    //密码修改
    public int UpdateAdminPWD(int sId,string sloginpwd)
    {
        OleDbConnection myconn = new OleDbConnection(DB.conn);
        OleDbCommand mycmd = new OleDbCommand("update [X-admin] set Apwd=@admin_pwd where Aid = @id", myconn);
        mycmd.Parameters.AddWithValue("@admin_pwd", sloginpwd);
        mycmd.Parameters.AddWithValue("@id", sId);
        int nResult = -1;
        try
        {
            myconn.Open();
            nResult = mycmd.ExecuteNonQuery();
        }
        catch (OleDbException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        finally
        {
            myconn.Close();
        }
        return nResult;
    }
    //
    //密码修改 根据用户名
    public int UpdateAdminPWD(string Auser, string sloginpwd)
    {
        OleDbConnection myconn = new OleDbConnection(DB.conn);
        OleDbCommand mycmd = new OleDbCommand("update [X-admin] set Apwd=@admin_pwd where Auser = @Auser", myconn);
        mycmd.Parameters.AddWithValue("@admin_pwd", sloginpwd);
        mycmd.Parameters.AddWithValue("@Auser", Auser);
        int nResult = -1;
        try
        {
            myconn.Open();
            nResult = mycmd.ExecuteNonQuery();
        }
        catch (OleDbException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        finally
        {
            myconn.Close();
        }
        return nResult;
    }
}
